local skill = fk.CreateSkill{
  name = "lvshi__collaborate_skill",
}

local DIY = require "packages/diy_utility/diy_utility"
local TT = require "packages/tuntian_studio/utility"

skill:addEffect("cardskill", {
  prompt = "#lvshi__collaborate_skill",
  can_use = Util.CanUse,
  min_target_num = 1,
  max_target_num = 2,
  mod_target_filter = function(self, player, to_select, selected)
    return not to_select:isKongcheng()
  end,
  target_filter = Util.CardTargetFilter,
  on_effect = function(self, room, effect)
    local from = effect.from
    local to = effect.to
    if from.dead or to.dead then return end
    local cid = room:askToChooseCard(from, { target = to, flag = "h", skill_name = "lvshi__collaborate" })
    if cid then
      if TT.isShown(cid) then
        DIY.hideCards(to, cid)
      else
        DIY.showCards(to, cid)
      end
    end
  end,
  on_action = function(self, room, use, finished)
    if finished then
      local cards = {}
      for _, p in ipairs(room.alive_players) do
        table.insertTable(cards, DIY.getShownCards(p))
      end
      if #cards > 1 and table.every(cards, function(id) return Fk:getCardById(cards[1]).color == Fk:getCardById(id).color end) then
        use.from:drawCards(2, "lvshi__collaborate")
      end
    end
  end,
})

return skill